var _ajax = require('../../utils/ajax');
var _Server = require('../../utils/server');
var WxParse = require('../../wxParse/wxParse'); // 富文本转化插件
Page({

    /**
     * 页面的初始数据
     */
    data: {
        switchBar: 1, // 讲师团队、专家团队
        age: '', // 年龄
        sex: '', // 性别
        selectBar: 1, // 综合排行、年龄、性别
        peopleIndex: 0, // 人物选择
        dropDownBool: false, // 查看更多下拉
        selectShow: false, // 是否显示筛选条件
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        // 获取列表
        that.fnAjaxInit();
    }, 
    fnAjaxInit: function (type,age,sex){ // 列表
        var that = this;
        _ajax.teacherList(that.data.switchBar, that.data.age, that.data.sex, function (data) {
            if (data.code == 0) {
                var data = data.data;
                that.setData({
                    indexData: data,
                    indexUrl: _Server.url,
                    selectShow: false,
                });
                if (data.length >= 1){
                    that.ajaxDetails(data[0].teacher_id);
                }
            }
        }, this);
    },
    ajaxDetails: function (teacher_id){ // 详情
        var that = this;
        _ajax.teacherInfo(teacher_id, function (data) {
            if (data.code == 0) {
                var data = data.data;
                var tags = '';
                if (data.tags){
                    tags = data.tags.split(',');
                }
                that.setData({
                    teachDetails: data,
                    tags: tags
                });
                var article = data.content;
                WxParse.wxParse('article', 'html', article, that, 5);
            }
        }, this);
    },
    fnSwitch: function(e) { // 讲师切换
        var that = this;
        if (e.target.dataset.num == that.data.switchBar){
            return;
        } else {
            that.setData({
                switchBar: e.target.dataset.num,
                selectShow: false,
                markIndex: -1,
                age: '',
                sex: '',
                selectBar: 1,
                peopleIndex: 0,
            });
            that.fnAjaxInit();
        }
    },
    fnSelectSwitch: function (e) { // 筛选切换
        var that = this;
        var bool = true;
        var num = e.target.dataset.num;
        if (num == 1){
            bool = false;
            this.setData({
                selectBar: num,
                selectShow: bool,
                age: '',
                sex: '',
                markIndex: -1,
            });
            that.fnAjaxInit();
        } else {
            this.setData({
                selectBar: num,
                selectShow: bool
            });
        }
    },
    fnPeopleSelect: function (e) { // 人物选择
        var that = this;
        var eData = e.currentTarget.dataset;
        that.ajaxDetails(eData.teachid);
        that.setData({
            peopleIndex: eData.peopleindex
        });
    },
    fndropDown: function (e) { // 查看更多
        this.setData({
            dropDownBool: !this.data.dropDownBool
        });
    },
    fnMarkSelect: function (e) { // 筛选选择 年龄、性别
        var that = this;
        var index = e.currentTarget.dataset.markindex;
        if (that.data.selectBar == 2){ // 年龄
            that.setData({
                markIndex: index,
                age: e.currentTarget.dataset.datapara,
                sex: '',
                peopleIndex: 0,
            });
        } else if (that.data.selectBar == 3){ // 性别
            that.setData({
                markIndex: index,
                age: '',
                sex: e.currentTarget.dataset.datapara,
                peopleIndex: 0,
            });
        }
        that.fnAjaxInit();
    },
    onShareAppMessage(res) { // 转发
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }
        return {
            title: '鹰之旅',
            path: '/pages/index/index'
        }
    }
})